package com.strawcom.charmring.util;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.strawcom.charmring.App;
import com.strawcom.charmring.R;

public class DialogUtil {

	private Context context;
	private Dialog dialog;
	private TextView txTitle;
	private Button btnConfirm, btnCancel;
	private View btnDivider;
	private View layoutTitle, layoutContent, layoutBtn;

	private DialogUtil() {
	}

	public static DialogUtil init(Context context) {
		DialogUtil dialogBuilder = new DialogUtil();
		dialogBuilder.context = context;
		return dialogBuilder;
	}

	public DialogUtil create(View content, boolean canCancel, boolean shadow) {
		dialog = new Dialog(context, shadow ? R.style.Theme_Light_NoTitle_Dialog : R.style.Theme_Light_NoTitle_NoShadow_Dialog);
		View view = LayoutInflater.from(context).inflate(R.layout.dialog_confirm, null);

		layoutTitle = view.findViewById(R.id.layout_title);
		layoutContent = view.findViewById(R.id.layout_content);
		layoutBtn = view.findViewById(R.id.layout_btn);

		txTitle = (TextView) view.findViewById(R.id.tx_title);
		btnConfirm = (Button) view.findViewById(R.id.btn_confirm);
		btnCancel = (Button) view.findViewById(R.id.btn_cancel);
		btnDivider = view.findViewById(R.id.btn_divider);

		((FrameLayout) layoutContent).addView(content);

		btnConfirm.setVisibility(View.GONE);
		btnDivider.setVisibility(View.GONE);

		btnCancel.setOnClickListener(new OnClickListener() {
			@Override public void onClick(View view) {
				if (dialog != null && dialog.isShowing()) dialog.dismiss();
				dialog = null;
			}
		});
		
		ViewUtil.changeFontsStyle(view, App.i());

		dialog.setContentView(view);
		dialog.setCanceledOnTouchOutside(canCancel);
		dialog.setCancelable(canCancel);

		return this;
	}

	public DialogUtil setConfirmListener(OnClickListener confirmListener) {
		btnConfirm.setVisibility(View.VISIBLE);
		btnDivider.setVisibility(View.VISIBLE);
		btnConfirm.setOnClickListener(confirmListener);
		return this;
	}
	
	public DialogUtil setCancelListener(OnClickListener cancelListener) {
		btnCancel.setOnClickListener(cancelListener);
		return this;
	}

	public DialogUtil setNoTitle() {
		layoutTitle.setVisibility(View.GONE);
		return this;
	}

	public DialogUtil setNoBtn() {
		layoutBtn.setVisibility(View.GONE);
		return this;
	}

	public DialogUtil setTitleText(int resId) {
		txTitle.setText(resId);
		return this;
	}

	public DialogUtil setConfirmText(int resId) {
		btnConfirm.setText(resId);
		return this;
	}

	public DialogUtil setCancelText(int resId) {
		btnCancel.setText(resId);
		return this;
	}

	public Dialog show() {
		dialog.show();
		return dialog;
	}

	public DialogUtil createSimple(int textId, boolean canCancel, boolean shadow) {
		TextView content = (TextView) LayoutInflater.from(context).inflate(R.layout.dialog_simple_text, null);
		content.setText(textId);
		return create(content, canCancel, shadow);
	}
	
	public DialogUtil createBottom(View content, boolean canCancel, boolean shadow) {
		create(content, canCancel, shadow);
		Window win = dialog.getWindow();
		win.setGravity(Gravity.BOTTOM);
		win.setWindowAnimations(R.style.Animation_Bottom_Rising);
		return this;
	}
}
